const install = (Vue, vm) => {
	/**
	 * 判断是否登陆
	 */
	const isLogin = () => {
		if(!vm.vuex_token){
			vm.$u.toast('请先登陆...');
			// 缓存当前页,用于登陆成功后跳转返回
			const currentPage = getCurrentPages().pop();
			const {options, route} = currentPage;
			
			const optionsKeys = Object.keys(options);
			let params = '';
			params = optionsKeys.reduce((pre, current) => {
				return `${pre}${current}=${options[current]}&`
			}, '?').slice(0,params.length-1);
			
			uni.setStorageSync("back_url", route + params);
			
			setTimeout(()=> {
				vm.$u.route({
					type: 'redirect',
					url: 'pages/auth/login'
				})
			}, 1500)
			return false;
		}
		
		return true;
	}
	
	const systemInfo = () => {
		// 请求API获取用户信息
		const res = uni.getSystemInfoSync();
				return res;
	}
	
	const refreshUserInfo = async () => {
		// 请求API获取用户信息
		const userInfo = await vm.$u.api.userInfo();
		// 缓存用户信息
		vm.$u.vuex('vuex_user', userInfo);
	}
	
	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.utils = {
		isLogin,
		refreshUserInfo,
		systemInfo
	};
}

export default {
	install
}